package 常用类.Date类;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class MyDate {
    public static void main(String[] args) throws ParseException {

    }
    public static void MyDate1() throws ParseException {

        /*
        第一类日期测试
         */
        Date date = new Date();
        System.out.println(date);
        //格式化时间对象
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");
        String format = simpleDateFormat.format(date);
        System.out.println(format);

        //将字符串转换为时间
        String str="2025年01月17日 10:17:36 周五";
        Date parse = simpleDateFormat.parse(str);
        System.out.println(parse);
        //日期转换为字符串
        String format1 = simpleDateFormat.format(parse);
        System.out.println(format1);
    }

    /*
    第二类时间测试
     */
    public static void MyDate2(){
        Calendar instance = Calendar.getInstance();
        System.out.println("instance = " + instance);
        System.out.println("年 = " + instance.get(Calendar.YEAR));
        System.out.println("月 = " + (instance.get(Calendar.MONTH)+1));
        System.out.println("日 = " + instance.get(Calendar.DAY_OF_MONTH));
        System.out.println("时" + instance.get(Calendar.HOUR));
        System.out.println("分" + instance.get(Calendar.MINUTE));
    }

    /*
    第三类时间测试
     */
    public static void MtDate3(){
        LocalDateTime now = LocalDateTime.now();
        System.out.println("now = " + now);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = formatter.format(now);
        System.out.println("时间为：" + format);
        System.out.println("年" + now.getYear());
        System.out.println("月" + now.getMonthValue());
        System.out.println("日" + now.getDayOfMonth());
    }
}
